﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Alfred.Ressource;

namespace Alfred
{
    class Rappel
    {
        public DateTime Date { get; set; }
        public string Message { get; set; }
        public string User { get; set; }
		
		public System.Timers.Timer timerRappel { get; set; }
		public List<Rappel> allRappels { get; set; }
		public List<Rappel> todayRappels { get; set; }

		public Rappel()
		{
			allRappels = new List<Rappel>();
			todayRappels = new List<Rappel>();
		}

		public void LaunchTimer()
		{
			timerRappel = new System.Timers.Timer(24 * 60 * 60000);
			timerRappel.Elapsed += new System.Timers.ElapsedEventHandler(TimerElapsed);
			timerRappel.Enabled = true;
		}

		public void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
		{
			GetTodaysRappel();
		}

		public void GetTodaysRappel()
		{
			todayRappels = allRappels.Where(r => r.Date.Day == DateTime.Now.Day).ToList();
		}

        public void GetAllRappel()
        {
            StreamReader s = new StreamReader(Paths.path_rappel);
            string ligne = s.ReadLine();

            while (ligne != null)
            {
                string[] infos = ligne.Split('\t');
                allRappels.Add(new Rappel() { User = infos[1], Date = DateTime.Parse(infos[0]), Message = infos[2] });
                ligne = s.ReadLine();
            }
            s.Close();
        }

        public void AddRappel(Rappel r)
        {
            StreamWriter s = new StreamWriter(Paths.path_rappel);
            string st = r.Date.ToShortDateString() + "\t" + r.User + "\t" + r.Message;
            s.Write(st);
            s.Close();
        }

        public string AllRappelsToString()
        {
            string str = "Vous avez " + allRappels.Count + " rappels : ";
			foreach (Rappel rappel in allRappels)
            {
                str += "Le " + rappel.Date.DayOfWeek.ToString() + " " + rappel.Date.Day + " " + rappel.Date.Month + " " + rappel.Date.Year + " " + rappel.Message;
            }
            return str;
        }

        public string TodayRappelToString()
        {
            string r = "Vous avez " + todayRappels.Count + " rappels aujourd'hui : ";

            foreach (Rappel rappel in todayRappels)
            {
                r += rappel.Message + ", ";
            }
            return r;
        }
    }
}
